#include "SeamWeldForm.h"
#include "ui_SeamWeldForm.h"

SeamWeldForm::SeamWeldForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SeamWeldForm)
{
    ui->setupUi(this);

    this->setWindowTitle("焊接参数查看");

    seam_model  = new QStringListModel;
    cad_model   = new QStringListModel;
    hseam_model = new QStringListModel;

    radioGroup = new QButtonGroup;
    radioGroup->addButton(ui->rb_h);
    radioGroup->addButton(ui->rb_a);


    ui->cb_seams->setModel(seam_model);
    ui->cb_hcad->setModel(cad_model);
    ui->cb_hseams->setModel(hseam_model);

    seam_model->setStringList(SysMembers::GetInstance()->seams);
    cad_model->setStringList(SysMembers::GetInstance()->cads);

    SysMembers::GetInstance()->moduleInitDone.connect([this](){
        seam_model->setStringList(SysMembers::GetInstance()->seams);
        cad_model->setStringList(SysMembers::GetInstance()->cads);
    });


}

SeamWeldForm::~SeamWeldForm()
{
    delete ui;
}

void SeamWeldForm::reflush()
{
    ui->le_endI->setText(QString::number(para.I_END));
    ui->le_endV->setText(QString::number(para.V_END));
    ui->le_weldI->setText(QString::number(para.I_W));
    ui->le_weldSpeed->setText(QString::number(para.S_W));
    ui->le_weldV->setText(QString::number(para.V_W));
    ui->le_freq->setText(QString::number(para.frequency));
    ui->le_range->setText(QString::number(para.range));
    ui->le_Time->setText(QString::number(para.T_END));

    ui->lb_ugpos->setText(QString::number(machine.angle1));
    ui->lb_rpos->setText(QString::number(machine.angle2));
    ui->lb_apos->setText(QString::number(machine.angle3));
    ui->le_serial->setText(QString::number(para.serialNumber));

}

void SeamWeldForm::on_pb_save_clicked()
{
    checkMerge();   // 先检查有没有改变过的


    if(weldparaNoChanged && offsetNoChanged){
        qDebug()<<"数据没有更新";
    }else {
        // 更新到数据库
        qDebug()<<"数据要更新了";
        pushMerge();
    }

}

void SeamWeldForm::on_pb_reflush_clicked()
{
    SysMembers::GetInstance()->update();
    reflush();
}

void SeamWeldForm::on_cb_seams_currentIndexChanged(const QString &arg1)
{
    bool ret ;
    int seam = arg1.toInt(&ret);
    if(!ret){
        QMessageBox::information(NULL, "焊缝转换错误", "无效的焊缝名称",
                                 QMessageBox::Yes );
        return;
    }



    updateWeldParas(seam);
    updateSeamWeldInfos(seam);
    updateSeamInfos(seam);
    updateSeamRelate(seam);

    reflush();
    ui->rb_a->setChecked(true);
}

/**
 * @brief SeamWeldForm::updateSeamWeldInfos
 * @param seam
 * @details 更新辅助焊接参数信息
 */
void SeamWeldForm::updateSeamWeldInfos(int seam)
{
    auto v = SQLHelper::GetBlade()->_p_tr->Get({"seamweldinfo"},
                {"offset","baseoffset"},
                {"seamid","'"+to_string(seam)+"'"});
    oldToolOffset  = v.at(0).toString();
    oldBaseOffset  = v.at(1).toString();


    ui->le_tooloff->setText(oldToolOffset);
    ui->le_baseoff->setText(oldBaseOffset);
}

void SeamWeldForm::updateSeamInfos(int seam)
{
    auto v = SQLHelper::GetBlade()->_p_tr->Get({"SEAMINFO"},
                {"cadid","orderid","seamtype","seamname"},
                {"seamid","'"+to_string(seam)+"'"});

    auto cadname  = v.at(0).toString(); // cad
    ui->lb_cadname->setText(cadname);
    auto order  = v.at(1).toString();  // 焊接次序
    ui->lb_weldorder->setText(order);
    auto lineType  = v.at(2).toString();    //焊缝类型
    ui->lb_seamtype->setText(lineType);

    auto seanname  = v.at(3).toString();    //焊缝名称
    ui->lb_seamname->setText(seanname);

    ui->lb_seamid->setText(QString::number(seam));

    auto m = SQLHelper::GetBlade()->_p_tr->Get({"SEAMINFO"},{"motorposs","motorangle1s","motorangle2s"},{"seamid","'"+to_string(seam)+"'"});

    machine.angle1 = m.at(0).toDouble();
    machine.angle2 = m.at(1).toDouble();
    machine.angle3 = m.at(2).toDouble();
}

void SeamWeldForm::updateSeamRelate(int seam)
{
    auto m = SQLHelper::GetBlade()->_p_tr->Get({"SEAMRELATE"},
    {"RELATE1","RELATE2"},
    {"seamid","'"+to_string(seam)+"'"});

    auto bind1 = m.at(0).toString();
    auto bind2 = m.at(1).toString();
    ui->lb_bind1->setText(bind1);
    ui->lb_bind2->setText(bind2);
}

void SeamWeldForm::updateWeldParas(int seam)
{
    bool ret;
    std::string query = "select * from weldparams where seamid=" + to_string(seam) +"";
    auto v = sqlProvider::GetInstance()->Get(query);

    para.I_W = v.at(2).toInt(&ret);
    para.S_W = v.at(1).toInt(&ret);
    para.V_W = v.at(3).toInt(&ret);
    para.T_END = v.at(4).toInt(&ret);
    para.I_END = v.at(5).toInt(&ret);
    para.V_END = v.at(6).toInt(&ret);

    para.range = v.at(7).toInt(&ret);
    para.axis_x = v.at(10).toInt(&ret);
    para.axis_y = v.at(11).toInt(&ret);

    para.frequency = v.at(8).toInt(&ret);
    para.serialNumber = v.at(9).toInt(&ret);
    // para.w = v.at(9).toInt(&ret);
}

void SeamWeldForm::checkMerge()
{
    weldparaNoChanged = true;
    weldparaNoChanged &= (ui->le_Time->text() == QString::number(para.T_END));
    weldparaNoChanged &= ( ui->le_weldI->text() == QString::number(para.I_W) );
    weldparaNoChanged &= ( ui->le_weldSpeed->text() == QString::number(para.S_W) );
    weldparaNoChanged &= ( ui->le_weldV->text() == QString::number(para.V_W) );
    weldparaNoChanged &= ( ui->le_endI->text() == QString::number(para.I_END) );
    weldparaNoChanged &= ( ui->le_endV->text() == QString::number(para.V_END) );
    weldparaNoChanged &= ( ui->le_range->text() == QString::number(para.range) );
    weldparaNoChanged &= ( ui->le_freq->text() == QString::number(para.frequency) );
    weldparaNoChanged &= ( ui->le_serial->text() == QString::number(para.serialNumber) );

    offsetNoChanged = true;
    offsetNoChanged &= ( ui->le_baseoff->text() == oldBaseOffset);
    offsetNoChanged &= ( ui->le_tooloff->text() == oldToolOffset );
}

void SeamWeldForm::pushMerge()
{
    QString upWeldPara("update weldparams set s_w=?,i_w=?,v_w=?,t_end=?,i_end=?,v_end=?,range=?,FREQUENCY=?,SERIALNUMBER=?");
    upWeldPara+= "where seamid='" + ui->lb_seamid->text() + "'";
    if(!weldparaNoChanged){

        QStringList ff;
        ff<<ui->le_weldSpeed->text()<<
                ui->le_weldI->text()<<
                ui->le_weldV->text()<<
                ui->le_Time->text()<<
                ui->le_endI->text()<<
                ui->le_endV->text()<<
                ui->le_range->text()<<
                ui->le_freq->text()<<
                ui->le_serial->text();
        SQLHelper::GetBlade()->_p_tr->insert(upWeldPara,ff );
    }

    QString upSeamWeld("update seamweldinfo set BASEOFFSET=?,OFFSET=?");
    upSeamWeld+= "where seamid='" + ui->lb_seamid->text() + "'";

    if(!offsetNoChanged){
        QStringList ff;
        ff<<ui->le_baseoff->text()<<
                ui->le_tooloff->text();
        SQLHelper::GetBlade()->_p_tr->insert(upSeamWeld,ff );
    }

    updateWeldParas(ui->lb_seamid->text().toUInt(new bool));
    updateSeamWeldInfos(ui->lb_seamid->text().toUInt(new bool));
    updateSeamInfos(ui->lb_seamid->text().toUInt(new bool));
    updateSeamRelate(ui->lb_seamid->text().toUInt(new bool));

    reflush();

}

void SeamWeldForm::on_cb_hcad_activated(const QString &arg1)
{
    auto t = SQLHelper::GetBlade()->_p_tr->Get({"SEAMINFO"},
    {"SEAMNAME"},
    {"CADID","'"+arg1.toStdString()+"'"});

    QStringList f;
    foreach (auto var, t) {
        auto d = var.toString();
        f << d;
    }
    hseam_model->setStringList(f);
}

void SeamWeldForm::on_cb_hseams_activated(const QString &arg1)
{
    auto t = SQLHelper::GetBlade()->_p_tr->Get({"SEAMINFO"},
    {"SEAMID"},
    {"SEAMNAME","'"+arg1.toStdString()+"'"});

    int seam = t.at(0).toInt(new bool);

    updateWeldParas(seam);
    updateSeamWeldInfos(seam);
    updateSeamInfos(seam);
    updateSeamRelate(seam);

    reflush();
    ui->rb_h->setChecked(true);
}
